<?php

namespace app\teacher\controller;

use app\common\controller\Frontend;
use app\common\enums\CourseFullTypeEnum;
use app\common\model\banxi\Grade;
use app\common\model\banxi\learn\LearnSkRecord;
use think\exception\ValidateException;
use think\facade\Validate;

/*
 * 个人数据统计
 * */
class PersonalData extends Frontend
{
    protected array $noNeedLogin = [''];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }

    /**
     * 查询-销课中心统计
     */
    public function getXkCenter()
    {
        $trainerId = $this->auth->id;

        $xkCenter = [
            'minute30Num' => 0,
            'minute60Num' => 0,
            'experienceNum' => 0,
            'totalLearn' => 0.0,
            'totalClassNum' => 0
        ];

        $LearnSkRecord = new LearnSkRecord();

        // 统计30分钟课程数量
        $xkCenter['minute30Num'] = $LearnSkRecord
            ->where('trainerId', $trainerId)
            ->where('isEnd', 1)
            ->where('courseFullType', CourseFullTypeEnum::C_30) // 30分钟课程
            ->count();

        // 统计60分钟课程数量
        $xkCenter['minute60Num'] = $LearnSkRecord
            ->where('trainerId', $trainerId)
            ->where('isEnd', 1)
            ->where('courseFullType', CourseFullTypeEnum::C_60) // 60分钟课程
            ->count();

        // 统计体验课数量
        $xkCenter['experienceNum'] = $LearnSkRecord
            ->where('trainerId', $trainerId)
            ->where('isEnd', 1)
            ->where('courseFullType', CourseFullTypeEnum::EXPERIENCE) // 体验课
            ->count();

        // 计算总学习时长（分钟转小时，保留2位小数）
        $totalMinutes = $LearnSkRecord
            ->where('trainerId', $trainerId)
            ->where('isEnd', 1)
            ->sum('learnMinutes');

        $xkCenter['totalLearn'] = round($totalMinutes / 60, 2);

        // 统计总上课次数
        $xkCenter['totalClassNum'] = $LearnSkRecord
            ->where('trainerId', $trainerId)
            ->where('isEnd', 1)
            ->count();

        $this->ok('操作成功', $xkCenter);
    }

}